package com.helloae.chap05;

import java.io.File;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 读取shp文件，并列出其字段的数量及各自的字段名。<br>
 * 示例章节：5.3.1，5.3.2
 * 
 * @author zhushijie
 * 
 */
public class ListOfFields {

	public static void main(String[] args) throws Exception {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty");
		IFields pFields = pFeatureClass.getFields();
		System.out.println("本图层中共有" + pFields.getFieldCount() + "个字段。");
		
		//列出所有字段名称
		for(int i = 0; i < pFields.getFieldCount(); i++){
			System.out.print(pFields.getField(i).getAliasName()+"\t");
			System.out.print(pFields.getField(i).getType()+"\t");
			System.out.print(pFields.getField(i).getLength()+"\t");
			System.out.print(pFields.getField(i).getPrecision()+"\t");
			System.out.print(pFields.getField(i).getScale());
			System.out.println();
		}

		System.out.println();
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
